<?php
class User extends AppModel {

    public $name = 'User';
    public $recursive = -1;
    public $actsAs = array('Acl'=>'requester');
    public $validate = array(
        'username' => array('notempty'),
        'password' => array('notempty'),
        'group_id' => array('notempty'),
        'file' => array(
            'resource'      => array('rule' => 'checkResource'),
            'access'          => array('rule' => 'checkAccess'),
            'size'                => array('rule' => array('checkSize', '5M')),
            'extension'     => array('rule' => array('checkExtension', false, array('xls', 'csv', 'tmp'))),
            'mimeType'      => array('rule' => array('checkMimeType', false, '*')))
    );

    public $belongsTo = array(
        'Group' => array(
            'className' => 'Group',
            'foreignKey' => 'group_id',
            'counterCache' => true,
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    // The Associations below have been created with all possible keys,
    // those that are not needed can be removed
    public $hasMany = array(
        'Attachment' => array(
            'className' => 'Media.Attachment',
            'foreignKey' => 'foreign_key',
            'conditions' => array('Attachment.model' => 'Student'),
            'dependent' => true
        ),
        'Post' => array(
            'className' => 'Post',
            'foreignKey' => 'user_id',
            'dependent' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Comment' => array(
            'className' => 'Comment',
            'foreignKey' => 'user_id',
            'dependent' => true,
            'conditions' => array('Comment.approved' => '1'),
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    public function afterSave($created){
        $aro =& $this->Aro;
        if($created){
            // its a creation
            $id = $this->getLastInsertID();
            $aro->updateAll(
                array('alias'=>'\'User:'.$id.'\''),
                array(
                    'Aro.model'=>'User',
                    'Aro.foreign_key'=>$id
                )
            );
        }else{
            // its an edit, we have to update the tree
            $data = $this->read();
            $parent_id = $data['User']['group_id'];
            $aro_record = $aro->findByAlias($this->name.':'.$this->id);
            $parent_record = $aro->findByAlias('Group:'.$parent_id);
            if(!empty($aro_record)){
                $parent_id = '0';
                if(!empty($parent_record)){
                    $parent_id = $parent_record['Aro']['id'];
                }
                // just changing parents
                $aro->save(array('parent_id' => $parent_id,'id' => $aro_record['Aro']['id']));
            }
        }
        return true;
    }
    public function parentNode(){
        // This should be the alias of the parent $model::$id
        $data = $this->read();
        // This needs to be unique
        return 'Group:'.$data['User']['group_id'];
    }

    /* Get File to import */
    public function getImportFile(){
        $user = $this->findByUsername('temp');
        if(!$user){
            return false;
        }else{
            return $this->Attachment->find('first', array(
                'conditions' => array(
                    'model' => 'User',
                    'foreign_key' => $user['User']['id']
                ),
                'order' => 'Attachment.created DESC'
            ));
        }
    }

    /* Update loggedin user status information, login_time, login_count */
    public function afterLogin($username){
        if(!$username){ return false; }
        /* We have to user direct database query, to avoid changing the User.modified value */
        $this->query('UPDATE users SET login_time=NOW() WHERE username="'.$username.'"');
        $this->query('UPDATE users SET login_count=login_count+1 WHERE username="'.$username.'"');
    }
}
?>